package com.china08.yxyapi.api.manage.edu.platform.slider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeSlider;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeSliderRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 焦点图管理 修改
 * 
 */
@CrossOrigin
@RestController
public class SliderUptController extends YxyApiBaseController {

    @Autowired
    private YxyHomeSliderRepository yhsr;

    @ApiOperation(value = "教育局 修改焦点图")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/slider/upd", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        String eduId = getProfile(request, response).getSchoolId();
        YxyHomeSlider homeSlider = yhsr.findByIdAndEdbId(reqModel.id, eduId);
        Assert.notNull(homeSlider, "焦点图不存在");
        homeSlider.setImg(reqModel.img);
        homeSlider.setUrl(reqModel.url);
        yhsr.save(homeSlider);
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 焦点图ID
        public String id;
        // 修改后的url （链接）
        public String url;
        // 修改后的图片路径
        public String img;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请选择要修改的焦点图");
            Assert.notNull(StringUtils.defaultIfBlank(img, null), "请上传焦点图");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}